<template>
  <view style="height: 100vh">

    <chat bind:queryCallback="getQueryCallback" bind:openWebview="openWebview"
      bind:openMiniProgram="openMiniProgram" generic:queryMessage="queryMessage"
      generic:textMessage="customTextMessage" generic:weatherMessage="customWeatherMessage"
      generic:imageMessage="customImageMessage" generic:musicMessage="customMusicMessage"
      generic:newsMessage="customNewsMessage" generic:unsupportedMessage="customUnsupportedMessage"
      generic:guideCard="customGuideCard" generic:operateCard="customoperateCard"
      generic:miniprogramMessage="customMiniprogramMessage" generic:h5Message="customh5Message"
      generic:welcomeCard="customWelcomeCard" generic:feedsCard="customFeedsCard">
    </chat>
  </view>
</template>

<script>
import { customerService } from '@/api/home/index.js'
import { getWechatCode } from '@/api/login/wechat.js'
export default {
  data () {
    return {
      link: '',
      wxCode: '',
      appid: '',
      openid: '',
      userHeader: '',
      userName: '',
      businessCode: '',
      purchaserName: '',
    }
  },

  onLoad () {
    getWechatCode().then(code => {
      this.wxCode = code
      customerService(this.wxCode).then(imRes => {
        if (imRes.code === 200) {
          this.link = imRes.data.h5
          this.appid = imRes.data.mpAppId
          this.openid = imRes.data.openId
          this.userHeader = imRes.data.userHeader
          this.userName = imRes.data.username
          this.businessCode = imRes.data.businessCode
          this.purchaserName = imRes.data.purchaserName

          var userNameContext = ""
          if (this.businessCode) {
            userNameContext = this.businessCode;
          }
          if (this.purchaserName) {
            userNameContext += "_" + this.purchaserName
          }
          if (this.userName) {
            userNameContext += "_" + this.userName
          }

          var plugin = requirePlugin("chatbot");
          plugin.init({
            appid: "2BPUP546fDQWsNiSBVG2Ibvpgns2bq",
            openid: this.openid, // 小程序的openid，必填项
            autoRecommendGuideList: true,
            userHeader: this.userHeader,
            userName: userNameContext,
            robotHeader: 'https://www.xdyy100.com/wholesale/files/avatar/robotHeader.png',
            background: "rgba(247,251,252,1)",
            guideCardHeight: 100,  //提示语距输入框高度
            operateCardHeight: 5,
            guideList: ["转人工", '发货问题', '怎么下单', '怎么支付'],
            textToSpeech: true,
            history: true,
            navHeight: 0,
            hideMovableButton: false,
            inputPlaceHolder: "请输入您的问题",
            noWaiterInfo: false,
            success: (res) => {
              console.log('res', res)
            },
            fail: (err) => {
              console.log('err', err)
            },
          })
        }
      })
    })
  },
  methods: {
    openWebview (e) {
      let url = e.detail.weburl
      wx.navigateTo({
        url: `/pages/webviewPage/webviewPage?url=${url}`
      })
    },
    openMiniProgram (e) {
      let { appid, pagepath } = e.detail
      if (appid) {
        wx.navigateToMiniProgram({
          appId: appid,
          path: pagepath,
          extraData: {},
          envVersion: "",
          success (res) {
            // 打开成功
            console.log(res)
          }
        });
      } else {
        wx.navigateTo({
          url: pagepath,
          fail () {
            wx.switchTab({
              url: pagepath
            });
          }
        });
      }
    },
    getQueryCallback (e) {
      console.log("---3->", e)
    },

  }
}
</script>

<style lang="scss" scoped>
</style>
